Skip to content

Conversation

@ehaas
Copy link
Contributor

@ehaas ehaas commented Oct 30, 2025

Add format(printf) attribute to modlog_printf to enable compile-time checking that format strings match arguments.

Note that this will cause incorrect modlog_printf usage which previously compiled successfully, to no longer compile.

@ehaas
Copy link
Contributor Author

ehaas commented Oct 30, 2025

Note that there is pre-existing usage of __attribute__(format) in sys/console/full/include/console/console.h so it seems like ensuring compatibility with compilers that do not support GNU-style attributes is not required?

@ehaas ehaas force-pushed the modlog-format-attribute branch 3 times, most recently from 1e70331 to acedf5a Compare October 30, 2025 23:50
@github-actions github-actions bot added size/s and removed size/xs labels Oct 30, 2025
@vrahane
Copy link
Contributor

vrahane commented Nov 3, 2025

@ehaas I think once the nimble PR is merged, the CI issues will get fixed. Let's wait for that.

@vrahane vrahane requested a review from sjanc November 4, 2025 21:32
Copy link
Contributor

@sjanc sjanc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what setup is giving you those errors where int32_t is long int? we could add this to CI to avoid similar issues in future

rc = mn_sendto(oc_ucast6, n, (struct mn_sockaddr *) &to);
if (rc != 0) {
OC_LOG_ERROR("Failed to send buffer %u on itf %d\n",
OC_LOG_ERROR("Failed to send buffer %u on itf %" PRIu32 "\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not a huge fan of PRI syntax... especially that it leads to inconsistent print formatting, eg here you don't use PRIu16 for omp_len

what do you think on using %u here and casting msin6_scope_id to (unsigned int) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated; I also find the PRI syntax pretty ugly.

*/
void modlog_printf(uint8_t module, uint8_t level, const char *msg, ...);
void modlog_printf(uint8_t module, uint8_t level, const char *msg, ...)
__attribute((format(printf, 3, 4)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modlog.h is included in a lot of modules in core, only osdp and oic require fixes for this to be enabled?

maybe it should be under MYNEWT_VAL (at least for time being) so that no unexpected build errors are introduced?

(especially that currently we always enable Werror - which we should make opt-in option anyway)

@sjanc
Copy link
Contributor

sjanc commented Nov 5, 2025

Note that there is pre-existing usage of __attribute__(format) in sys/console/full/include/console/console.h so it seems like ensuring compatibility with compilers that do not support GNU-style attributes is not required?

yeah, I guess any reasonable compiler supports this extension :)

@ehaas
Copy link
Contributor Author

ehaas commented Nov 10, 2025

what setup is giving you those errors where int32_t is long int? we could add this to CI to avoid similar issues in future

~$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 8-2018-q4-major) 8.2.1 20181213 (release) [gcc-8-branch revision 267074]

Here is a repro on arm-none-eabi-gcc 8.3.1: https://godbolt.org/z/sTnPahqE5

@ehaas ehaas force-pushed the modlog-format-attribute branch from acedf5a to de2fb82 Compare November 11, 2025 23:42
Copy link
Contributor

@sjanc sjanc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks ok to me but please fix compliance issues reported

I guess Nimble PR also should be updated to use cast instead PRI and this should be ready for merging

@vrahane
Copy link
Contributor

vrahane commented Nov 14, 2025

@ehaas commit body is required by mynewt, can you please update the commit with the body.

Checking commits from e77638680bd8c6de3e43694683db045dde7eed99 to HEAD
Commit b2c390b body is missing

Commit b12aa4a body is missing

Add format(printf) attribute to modlog_printf to enable compile-time
checking that format strings match arguments. By default the
attribute is disabled; it can be enabled by setting
MODLOG_USE_PRINTF_ATTRIBUTE to 1.

Note that enabling the attribute will cause incorrect modlog_printf
usage which previously compiled successfully, to no longer compile.
@ehaas ehaas force-pushed the modlog-format-attribute branch from b2c390b to 1764a28 Compare November 14, 2025 21:08
Copy link
Contributor

@vrahane vrahane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all comments have been addressed, I think we are good to merge this in.

@vrahane vrahane merged commit 6267e64 into apache:master Nov 14, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants